<?php
/**
 * Created by PhpStorm.
 * User: kajun
 * Date: 2017/4/7
 * Time: 21:28
 */
namespace Admin\Controller;

use Think\Page;
use Think\Upload;

class OtherController extends AdminController
{
    public function index()
    {
        $this->redirect('carousel');
    }

    public function carousel()
    {
        $p = I('p', '1');
        $carousel = M('Carousel');
        $page_size = 20;
        $count = $carousel->count();
        $list = $carousel->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }


    public function carousel_add()
    {
        $this->display();
    }

    public function carousel_edit()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $carousel = M('Carousel');
            $count = $carousel->where($where)->count();
            if ($count) {
                $data = $carousel->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function carousel_update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $url = I('url', '');
        $outline = I('outline', '');
        $show = I('show', '0');
        $create_time = date('Y-m-d H:i:s');

        $data = array();
        $data['title'] = $title;
        $data['show'] = $show;
        $data['outline'] = $outline;
        $data['create_time'] = $create_time;
        $data['url'] = $url;

        $upload = new Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '/Picture/Admin/'; // 设置附件上传目录
        $file = $upload->upload();
        if ($file) {
            $url_t = $file['image']['savepath'] . $file['image']['savename'];
            $data['url'] = $url_t;
        } else if ($url == '') {
            $this->error($upload->getError());
        }
        $carousel = M('Carousel');

        if (empty($id)) {
            $res = $carousel->data($data)->add();
            if ($res) {
                $this->success("添加成功", U('carousel'));
            } else {
                $this->error("添加失败", U('carousel'));
            }
        } else {

            $res = $carousel->where(array('id' => $id))->data($data)->save();
            if ($res) {
                $this->success("修改成功", U('carousel'));
            } else {
                $this->error("修改失败", U('carousel'));
            }
        }
    }

    public function carousel_active()
    {
        $id = I('id', '');
        $show = I('show', '');
        $where = array('id' => $id);
        if (!empty($id) && ($show == '1' || $show == '0')) {
            $count = M('Carousel')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Carousel')->where($where)->data(array('show' => $show))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }

    public function carousel_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Carousel')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        } else {
            $this->error("删除失败");
        }
    }

    public function server()
    {
        $p = I('p', '1');
        $page_size = 20;
        $servers = M('Server');
        $count = $servers->count();
        $list = $servers->page($p . ',' . $page_size)->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function server_add()
    {
        $this->display();
    }

    public function server_edit()
    {
        $id = I('id', '');
        $where = array('id' => $id);
        $server = M('Server');
        if (!empty($id)) {
            $count = $server->where($where)->count();
            if ($count) {
                $data = $server->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function server_update()
    {
        $id = I('id', '');
        $name = I('name', '');
        $qq_number = I('qq_number', '');
        $show = I('show', '0');

        $data['name'] = $name;
        $data['qq_number'] = $qq_number;
        $data['show'] = $show;

        $server = M('Server');

        if (empty($id)) {
            $res = $server->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('server'));
            } else {
                $this->error('添加失败!', U('server'));
            }
        } else {
            $where['id'] = $id;
            $count = $server->where($where)->count();
            if ($count) {
                $res = $server->where($where)->data($data)->save();
                if ($res) {
                    $this->success('更新成功', U('server'));
                } else {
                    $this->error('更新失败!', U('server'));
                }
            } else {
                $this->error('更新失败!', U('server'));
            }
        }
    }

    public function server_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Server')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        } else {
            $this->error("删除失败");
        }
    }


    public function server_active()
    {
        $id = I('id', '');
        $show = I('show', '');
        $where = array('id' => $id);
        if (!empty($id) && ($show == '1' || $show == '0')) {
            $count = M('Server')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Server')->where($where)->data(array('show' => $show))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }

    public function video()
    {
        $p = I('p', '1');
        $video = M('Video');
        $page_size = 20;
        $count = $video->count();
        $list = $video->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function video_add()
    {
        $this->display();
    }

    public function video_edit()
    {
        $id = I('id', '');
        $where = array('id' => $id);
        $video = M('Video');
        if (!empty($id)) {
            $count = $video->where($where)->count();
            if ($count) {
                $data = $video->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function video_update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $url = I('url', '');
        $show = I('show', '0');
        $create_time = date('Y-m-d H:i:s');

        $data = array();
        $data['title'] = $title;
        $data['url'] = $url;
        $data['show'] = $show;
        $data['create_time'] = $create_time;
//
//        $upload = new Upload();// 实例化上传类
//        $upload->maxSize = 3145728000;// 设置附件上传大小
//        $upload->exts = array('ogg', 'mp4');// 设置附件上传类型
//        $upload->savePath = '/Video/'; // 设置附件上传目录
//        $file = $upload->upload();
//        if ($file) {
//            $url_t = $file['video']['savepath'] . $file['video']['savename'];
//            $data['url'] = $url_t;
//        } else if ($url == '') {
//            $this->error($upload->getError());
//        }

        $video = M('Video');

        if (empty($id)) {
            $res = $video->data($data)->add();
            if ($res) {
                $this->success("添加成功", U('video'));
            } else {
                $this->error("添加失败", U('video'));
            }
        } else {
            $where['id'] = $id;
            $res = $video->where($where)->data($data)->save();
            if ($res) {
                $this->success("修改成功", U('video'));
            } else {
                $this->error("修改失败", U('video'));
            }
        }
    }

    public function video_active()
    {
        $id = I('id', '');
        $show = I('show', '');
        $where = array('id' => $id);
        if (!empty($id) && ($show == '1' || $show == '0')) {
            $count = M('Video')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Video')->where($where)->data(array('show' => $show))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }

    public function video_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Video')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功!");
            } else {
                $this->error("删除失败！");
            }
        } else {
            $this->error("删除失败");
        }
    }

    public function partner()
    {
        $p = I('p', '1');
        $carousel = M('Partner');
        $page_size = 20;
        $count = $carousel->count();
        $list = $carousel->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }


    public function partner_add()
    {
        $this->display();
    }

    public function partner_edit()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $partner = M('Partner');
            $count = $partner->where($where)->count();
            if ($count) {
                $data = $partner->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function partner_update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $type = I('type', '');
        $addurl = I('addurl', '');
        $url = I('url', '');
        $create_time = date('Y-m-d H:i:s');

        $data = array();
        $data['title'] = $title;
        $data['type'] = $type;
        $data['addurl'] = $addurl;
        $data['url'] = $url;
        $data['create_time'] = $create_time;

        $upload = new Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '/Picture/Partner/'; // 设置附件上传目录
        $file = $upload->upload();
        if ($file) {
            $url_t = $file['image']['savepath'] . $file['image']['savename'];
            $data['url'] = $url_t;
        } else if ($url == '') {
            $this->error($upload->getError());
        }
        $partner = M('Partner');

        if (empty($id)) {
            $res = $partner->data($data)->add();
            if ($res) {
                $this->success("添加成功", U('partner'));
            } else {
                $this->error("添加失败", U('partner'));
            }
        } else {

            $res = $partner->where(array('id' => $id))->data($data)->save();
            if ($res) {
                $this->success("修改成功", U('partner'));
            } else {
                $this->error("修改失败", U('partner'));
            }
        }
    }

    public function partner_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Partner')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        } else {
            $this->error("删除失败");
        }
    }

    public function adv()
    {
        $p = I('p', '1');
        $advertisement = M('Advertisement');
        $page_size = 20;
        $count = $advertisement->count();
        $list = $advertisement->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }


    public function adv_add()
    {
        $this->display();
    }

    public function adv_edit()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $advertisement = M('Advertisement');
            $count = $advertisement->where($where)->count();
            if ($count) {
                $data = $advertisement->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->error('查找失败');
        }
    }

    public function adv_update()
    {
        $id = I('id', '');
        $title = I('title', '');
        $url = I('url', '');
        $outline = I('outline', '');
        $show = I('show', '0');
        $create_time = date('Y-m-d H:i:s');

        $data = array();
        $data['title'] = $title;
        $data['show'] = $show;
        $data['url'] = $url;
        $data['outline'] = $outline;
        $data['create_time'] = $create_time;

        $upload = new Upload();// 实例化上传类
        $upload->maxSize = 20 * 1024 * 1024;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '/Picture/Adv/'; // 设置附件上传目录
        $file = $upload->upload();
        if ($file) {
            $url_t = $file['image']['savepath'] . $file['image']['savename'];
            $data['url'] = $url_t;
        } else if ($url == '') {
            $this->error($upload->getError());
        }
        $advertisement = M('Advertisement');

        if (empty($id)) {
            $res = $advertisement->data($data)->add();
            if ($res) {
                $this->success("添加成功", U('adv'));
            } else {
                $this->error("添加失败", U('adv'));
            }
        } else {

            $res = $advertisement->where(array('id' => $id))->data($data)->save();
            if ($res) {
                $this->success("修改成功", U('adv'));
            } else {
                $this->error("修改失败", U('adv'));
            }
        }
    }

    public function adv_active()
    {
        $id = I('id', '');
        $show = I('show', '');
        $where = array('id' => $id);
        if (!empty($id) && ($show == '1' || $show == '0')) {
            $count = M('Advertisement')->where($where)->count();
            if ($count == 0) {
                $this->error("不存在该记录");
            } else {
                $res = M('Advertisement')->where($where)->data(array('show' => $show))->save();
                if ($res) {
                    $this->success("状态修改成功");
                } else {
                    $this->error("状态修改失败");
                }
            }
        } else {
            $this->error("不存在该记录");
        }
    }

    public function adv_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Advertisement')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        } else {
            $this->error("删除失败");
        }
    }

    public function advice()
    {
        $p = I('p', '1');
        $advices = M('Advices');
        $page_size = 20;
        $count = $advices->count();
        $list = $advices->page($p . ',' . $page_size)->order('create_time desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function advice_detail()
    {
        $id = I('id', '');
        $advice = M('Advices')->where('id=' . $id)->find();
        if ($advice) {
            $this->assign('data', $advice);
            $this->display();
        } else {
            $this->error('不存在该建议!');
        }
    }

    public function advice_delete()
    {
        $id = I('id', '');
        if (!empty($id)) {
            $res = M('Advices')->where(array('id' => $id))->delete();
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        } else {
            $this->error("删除失败");
        }
    }

    public function link()
    {
        $link = M('Link')->order('create_time desc')->select();
        $this->assign('link', $link);
        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            $link = M('Link');
            $data['title'] = I('title', '');
            $data['url'] = I('url', '');
            $data['status'] = I('status', '');
            $data['target'] = I('target', '');
            $data['create_time'] = time();
            $res = $link->add($data);
            if ($res) {
                $this->success('添加成功!', U('link'));
            } else {
                $this->error('添加失败!', U('link'));
            }
        } else {
            $this->meta_title = '新增友情链接';
            $this->display('edit');
        }
    }

    public function edit($id = 0)
    {
        $link = M('Link');
        if (IS_POST) {
            $data['id'] = $id;
            $data['title'] = I('title', '');
            $data['url'] = I('url', '');
            $data['status'] = I('status', '');
            $data['target'] = I('target', '');
            if ($link->save($data)) {
                $this->success('修改成功!', U('link'));
            } else {
                $this->error('修改失败!', U('link'));
            }
        } else {
            $info = $link->where("id='$id'")->find();
            $this->assign('info', $info);
            $this->meta_title = '编辑友情链接';
            $this->display();
        }
    }

    public function del($id = 0)
    {
        $map = array('id' => array('eq', $id));
        if (M('link')->where($map)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    public function active($id = 0)
    {
        $map = array('id' => $id);
        $status = I('status', 0);
        if ($status == 0 || $status == 1) {
            if (M('link')->where($map)->save(array('status'=>$status))) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败！');
            }
        }
    }


    public function album(){
        $p = I('p', '1');
        $album = M('Album');
        $page_size = 20;
        $count = $album->count();
        $list = $album->page($p . ',' . $page_size)->order('createtime desc')->select();
        $page = new Page($count, $page_size);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    public function album_edit(){
        $id = I('id', '');
        if (!empty($id)) {
            $where = array('id' => $id);
            $album = M('Album');
            $count = $album->where($where)->count();
            if ($count) {
                $data = $album->where($where)->find();
                $this->assign('data', $data);
                $this->display();
            } else {
                $this->error('查找失败');
            }
        } else {
            $this->display();
        }
    }

    public function album_save(){
        $id = I('id', '');
        $title = I('title', '');
        $coverId = I('coverId', '');
        $content = I('content', '');
        $status = I('status', '0');

        $data['title'] = $title;
        $data['coverId'] = $coverId;
        $data['content'] = $content;
        $data['status'] = $status;

        $album = M('Album');

        if (empty($id)) {
            $data['createtime'] = time();
            $res = $album->data($data)->add();
            if ($res) {
                $this->success('添加成功!', U('album'));
            } else {
                $this->error('添加失败!', U('album'));
            }
        } else {
            $where['id'] = $id;
            $count = $album->where($where)->count();
            if ($count) {
                $res = $album->where($where)->data($data)->save();
                if ($res) {
                    $this->success('更新成功！', U('album'));
                } else {
                    $this->error('更新失败!', U('album'));
                }
            } else {
                $this->error('更新失败!', U('album'));
            }
        }
    }

    public function album_active($id = 0){
        $map = array('id' => $id);
        $status = I('status', 0);
        if ($status == 0 || $status == 1) {
            if (M('Album')->where($map)->save(array('status'=>$status))) {
                $this->success('修改成功！');
            } else {
                $this->error('修改失败！');
            }
        }
    }

    public function album_del($id=0){
        $map = array('id' => array('eq', $id));
        if (M('Album')->where($map)->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
}